<?php

namespace humhub\modules\api\models;

use Yii;


class Scores extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'scores_list';
    }

    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    public function setScores($goods='任务奖励',$score=0,$gid='',$type=0,$sign=0){
        $uid=Yii::$app->user->id;
        $this->uid=$uid;
        $this->gid=$gid;
        $this->goods=$goods;
        $this->score=$score;
        $this->time=date('Y-m-d H:i:s',time());

        $this->save(false);
        $user=User::findOne($uid);

        if ($type==1){
            $user->last_sign=time();
            if ($sign==1){
                $user->sign_count=1;
            }else{
                $user->sign_count +=1;
            }
        }

        $user->u_score +=$score;
        $user->save(false);
    }

}
